/**
 * 
 */
package org.t2.backstage.module.recharge;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.backstage.interceptor.RoleContext;
import org.gaming.backstage.module.user.service.UserService;
import org.gaming.service.advice.ResponseResult;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.service.asserts.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.TipsCode;
import org.t2.backstage.module.gameserver.model.GameServer;
import org.t2.backstage.module.gameserver.service.GameServerService;
import org.t2.backstage.module.player.model.Player;
import org.t2.backstage.module.player.service.PlayerService;
import org.t2.backstage.module.recharge.form.RechargeVirtualForm;
import org.t2.backstage.module.recharge.model.RechargeProductCfg;
import org.t2.backstage.module.recharge.model.RechargeVirtual;
import org.t2.backstage.module.recharge.service.RechargeProductCfgService;
import org.t2.backstage.module.recharge.service.RechargeVirtualService;
import org.t2.backstage.module.recharge.struct.RechargeVirtualQuery;
import org.t2.backstage.util.T2HttpUtil;

import sh.t2.share.apiutil.GameApi;
import sh.t2.share.module.recharge.RechargeApiProtocol.RechargeCallVirtualRequest;

/**
 * @author YY
 *
 */
@Controller
public class RechargeVirtualController {

	@Autowired
	private RechargeVirtualService rechargeVirtualService;
	@Autowired
	private PlayerService playerService;
	@Autowired
	private UserService userService;
	@Autowired
	private GameServerService gameServerService;
	@Autowired
	private RechargeProductCfgService rechargeProductCfgService;
	
	
	@ApiDocument("请求虚拟充值（内部充值）数据")
	@RequestMapping(value = "/rechargeVirtual/data.auth")
	@ResponseBody
	public PageData<RechargeVirtual> data(RechargeVirtualQuery query) {
		QuerySet querySet = new QuerySet();
		if(query.getOpenId() != null && !"".equals(query.getOpenId())) {
			querySet.addCondition("open_id like ?", "%" + query.getOpenId() + "%");
		}
		if(query.getPlayerId() > 0) {
			querySet.addCondition("player_id = ?", query.getPlayerId());
		}
		if(query.getNick() != null && !"".equals(query.getNick())) {
			querySet.addCondition("nick like ?", "%" + query.getNick() + "%");
		}
		querySet.orderBy("order by id desc");
		querySet.limit(query.getPage(), query.getLimit());
		querySet.formWhere();
		
		return rechargeVirtualService.query(querySet);
	}
	
	@ApiDocument("发放虚拟充值（内部充值）")
	@RequestMapping(value = "/rechargeVirtual/add.authw")
	@ResponseBody
	public RechargeVirtual add(RechargeVirtualForm form) {
		RoleContext roleContext = userService.getCurrUser();
		
		Player player = playerService.getEntity(form.getPlayerId());
		Asserts.isTrue(player != null, TipsCode.PLAYER_NOT_EXIST, form.getPlayerId());
		
		GameServer gameServer = gameServerService.getByPlatformAndServer(player.getPlatformId(), player.getServerId());
		
		RechargeVirtual rechargeVirtual = new RechargeVirtual();
		rechargeVirtual.setChannelId(player.getChannelId());
		rechargeVirtual.setPlayerId(player.getId());
		rechargeVirtual.setNick(player.getNick());
		rechargeVirtual.setOpenId(player.getOpenId());
		rechargeVirtual.setProductId(form.getProductId());
		
		RechargeProductCfg rechargeCfg = rechargeProductCfgService.getEntity(0, form.getProductId());
		if(rechargeCfg != null) {
			rechargeVirtual.setProductName(rechargeCfg.getName());
			rechargeVirtual.setMoney(rechargeCfg.getPrice());
		}
		
		rechargeVirtual.setServerUid(gameServer.getId());
		rechargeVirtual.setServerId(gameServer.getServerId());
		rechargeVirtual.setServerName(gameServer.getName());
		rechargeVirtual.setUserId(roleContext.getId());
		rechargeVirtual.setUserName(roleContext.getName());
		rechargeVirtual.setRemark(form.getRemark());
		rechargeVirtualService.insert(rechargeVirtual);
		
		RechargeCallVirtualRequest request = new RechargeCallVirtualRequest();
		request.playerId = form.getPlayerId();
		request.productId = form.getProductId();
		ResponseResult<String> serverResponse = T2HttpUtil.jsonPost(gameServer, GameApi.virtualRecharge, request, String.class);
		if(serverResponse == null) {
			rechargeVirtual.setStatus(2);
		} else if(serverResponse.getCode() == 0) {
			rechargeVirtual.setStatus(1);
		} else {
			rechargeVirtual.setStatus(2);
		}
		rechargeVirtualService.update(rechargeVirtual);
		
		return rechargeVirtual;
	}
}
